import React, {Component} from 'react';
import OrgRefer from '../../../../../../hrpub/common/components/referSearch/org';
import NCBackBtn from 'src/hrpub/common/components/hr-back';
import "../../../../../../hrpub/common/static/fonts/iconfont.css";
import './index.less'
class Header extends Component {
    constructor(props) {
        super(props);
        this.state = {
            pk_org: '',
            orgValue: {},
            backBtnVisible: false,
            paginationShow: false,
            cardPaginationDis: true
        }
    }
    // 组织参照发生改变触发
    handleTreeChange(value) {
        this.setState({
            pk_org: value.pk_org,
            orgValue: value
        }, function () {
            setTimeout(() => {
                this.props.checkOrDeleteOrg(value.refpk)
            }, 100)
        })
    }
    // 点击各按钮处理函数
    buttonClickHandle (props, btnCode) {
        this.props.BtnOperationFun(btnCode)
    }
    // 设置返回按钮显示隐藏
    setBackBtnVisible (backBtnVisible) {
        this.setState({
            backBtnVisible
        })
    }
    // 点击返回按钮
    backBtnClick () {
        let {button} = this.props
        let btnData = button.getButtons()
        let saveBtnStatus = null
        let props = this.props
        for (let i=0; i < btnData.length; i++) {
            if (btnData[i].key === 'save') {
                saveBtnStatus = btnData[i].visible
                break
            }
        }
        if (saveBtnStatus) {
            this.props.cancelOpreation()
            return false
        }
        props.setCardListVisible('tableList')
        this.setBackBtnVisible(false)
        this.setCardPaginationConDis(false)
        props.setBtnVisibleAndDisable('addDeleteMakeEffectRefreshQuery')
        props.queryTableList()
    }
    handlePageInfoChange (props, flag, status) {
        props.cardPagination.setCardPaginationId({ id: flag, status: status });
        this.props.initDataShow(flag)
        let record = {
            values: {pk_psndoc_sub: {value: flag}}
        }
        this.props.setRecord(record)
    }
    // 设置卡片容器显示隐藏
    setCardPaginationConDis (cardPaginationDis) {
        this.setState({
            cardPaginationDis
        })
    }
    // 设置卡片分页显示隐藏
    setCardPaginationVisible (cardPaginationDis, paginationShow, pk_psndoc_sub) {
        this.setState({
            cardPaginationDis,
            paginationShow
        }, function () {
           if (pk_psndoc_sub) {
                let {cardPagination} = this.props
                cardPagination.setCardPaginationId({id: pk_psndoc_sub, status: 1});
           }
        })
    }
    // 给人力组织赋值
    setOrgValue (orgValue) {
        this.setState({
            orgValue
        })
    }
    render () {
        let {button, cardPagination} = this.props
        let {createCardPagination} = cardPagination
        return (
            <div className="header nc-bill-header-area">
                <div className="header-panel left">
                    {
                        this.state.backBtnVisible ? 
                        <NCBackBtn 
                            // title={'返回'}
                            onClick={this.backBtnClick.bind(this)}/> :
                        <OrgRefer
                            getOrgData={this.handleTreeChange.bind(this)}
                            orgVal={this.state.orgValue}
                        />
                    }
                </div>
                <div className="btn-group">
                    {
                        button.createButtonApp({
                            area: 'head',
                            onButtonClick: this.buttonClickHandle.bind(this)
                        })
                    }
                    <div className="cardPagination" style={{display: this.state.cardPaginationDis ? 'inline-block' : 'none'}}>
                        {
                            this.state.paginationShow ? createCardPagination({
                                handlePageInfoChange: this.handlePageInfoChange.bind(this),
                                urlPkname: 'cardPaginationExtend'
                            }) : ''
                        }
                    </div>
                </div>
            </div>
        )
    }
}

export default Header